<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
error_reporting(E_ERROR | E_WARNING | E_PARSE);//容错
/**
 * @param $pwd 管理员登录密码
 * @param $phone 管理员账号
 * @return string
 */
define('PWDSALT','BOCP2P');
define('HMACPWD', md5(PWDSALT.md5(PWDSALT.'HMACPWD'))); // PASSWD and cookie
function passwd($pwd){
    return md5($pwd.HMACPWD);
}

/**
 * @param string $url 上传地址
 * @param array $data 上传数组
 * @param string $header 头文件设置
 */
function set_curl($url, $data = array(), $header = '')
{
    $ch = curl_init();
    if (!empty($header)) {
//            $header=array('Content-type: application/json','charset:utf-8'); 示例
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
    curl_setopt($ch, CURLOPT_URL, $url);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POST, '1');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //禁用后cURL将终止从服务端进行验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $res = curl_exec($ch);
    if (curl_error($ch)) {
        return '错误信息:' . curl_error($ch);
    }
    curl_close($ch);
    return $res;
}
//获取IP 地址
function getIP(){
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $IPaddress = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $IPaddress = getenv("HTTP_CLIENT_IP");
        } else {
            $IPaddress = getenv("REMOTE_ADDR");
        }
    }
    return $IPaddress;
}
/**
* 模拟post进行url请求
* @param string $url
* @param array $post_data
*/
function request_post($url = '', $post_data = array()) {
    if (empty($url) || empty($post_data)) {
        return false;
    }

    // $o = "";
    // foreach ($post_data as $k => $v ) {
    //     $o.= "$k=" . urlencode( $v ). "&" ;
    // }
    // $post_data = substr($o,0,-1);
    $postUrl = $url;
    $curlPost = $post_data;
    //var_dump($curlPost);die;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}